home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr11 / powerb5.zip / P5DOS006.TIP < prev    next >
Text File  |  1993-06-01  |  2KB  |  60 lines

  1. The FOR statement makes DOS run the same command on each of
  2. a list of files. For instance, you might type FOR %F IN
  3. (*.DOC *.TXT) DO TYPE %F to display every DOC and TXT file
  4. in the current directory. The trouble is that FOR can run
  5. only one command on each of the listed files. If you want to
  6. list and copy all the DOC and TXT files, forget it. FOR
  7. doesn't let you run that kind of loop.
  8.  
  9. I've discovered a solution. You can create a batch file that
  10. recursively calls itself and then jumps to a subroutine
  11. within itself every time FOR acts on a file. An example,
  12. FORTEST.BAT, is attached.
  13.  
  14. Michael Sigmundt
  15. Willowdale, Ontario, Canada
  16.  
  17. Editor's Note: While the FOR command does allow you to
  18. execute a CALL command to run multiple commands on each file
  19. in a list, you normally need to establish a separate batch
  20. file to be called. This technique lets you put everything in
  21. one batch file. To see how it works, simply run FORTEST at
  22. the DOS prompt. Note how the 'ECHO Once' and 'ECHO Twice'
  23. commands (these substitute in this example for whatever real
  24. commands you may need) work on each file in the directory.
  25.  
  26. This trick essentially allows you to implement a subroutine
  27. -- something DOS's batch language can't normally do. (DR DOS
  28. and most third-party shells can.) A special value of the
  29. first parameter acts as a signal to the batch file, which
  30. can jump to the subroutine code on entry. Note Mr.
  31. Sigmundt's clever use of the SHIFT command to eliminate the
  32. special parameter once the subroutine is entered.
  33.  
  34. FORTEST.BAT
  35.  
  36. ---- BEGIN LISTING ----
  37. @ECHO OFF
  38. IF "%1"=="#LOOP#" GOTO LOOP
  39. ECHO This is the beginning....
  40. FOR %%F IN (*.*) DO CALL FORTEST.BAT #LOOP# %%F
  41. ECHO This is the end....
  42. GOTO END
  43.  
  44. :LOOP
  45. REM Subroutine portion here
  46. SHIFT
  47. ECHO Once with %1
  48. ECHO Twice with %1
  49.  
  50. :END
  51. ---- END LISTING ----
  52.  
  53.  
  54. Title: Batch Files that Loop the Loop
  55. Category: DOS
  56. Issue Date: August, 1992
  57. Editor: Brett Glass
  58. Supplementary Files: None
  59. Filename: P5DOS006.TIP
  60.